﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig.Controller
{
    public  class PrinterController
    {
        #region //private fields

        private PrinterBLL  _bll = new PrinterBLL();
        private GridControl _grid = null;
        private GridView _view = null;
        private ComboBoxEdit _comboBox = null;
        private GroupControl _groupControl = null;
        private CheckEdit _checkEdit = null;

        #endregion


        #region //Public Properties

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        #region //Public Methods

        /// <summary>
        /// Get Printers when the form loadding
        /// </summary>
        public void GetDate()
        {
            _grid.DataSource = _bll.GetPrinterList();
        }

        /// <summary>
        /// Add shops
        /// </summary>
        /// <param name="obj"></param>
        public void AddPrinter(PrinterInfo obj,string paperFaceName,string paperTypeName,int sharping)
        {
            _bll.AddPrinter(obj,paperFaceName,paperTypeName,sharping);
        }

        /// <summary>
        /// Update shop
        /// </summary>
        /// <param name="obj"></param>
        public void UpdatePrinter(PrinterInfo obj, string paperFaceName, string paperTypeName, int sharping)
        {
            _bll.UpdatePrinter(obj,paperFaceName,paperTypeName,sharping);
        }

        /// <summary>
        /// Delete shop
        /// </summary>
        /// <param name="obj"></param>
        public void DeletePrinter(PrinterInfo obj)
        {
            _bll.DeletePrinter(obj);
        }

        public printer GetPrinterObj(string name)
        {
            return _bll.GetPrinterObj(name);
        }

        public GridSubArea GetSubAreaObj(string name)
        {
            AreaBLL bll = new AreaBLL();
            return bll.GetAreaObj(name);
        }
        public void LinkSubArea()
        {
            _comboBox.Properties.Items.Clear();
            AreaBLL bll = new AreaBLL();
            List<GridSubArea> list = bll.GetAreaList();
            foreach (GridSubArea obj in list)
            {
                _comboBox.Properties.Items.Add(obj.SubAreaName);
            }
        }

        public void LinkPaperFace()     
        {
            PaperFaceBLL bll = new PaperFaceBLL();
            _comboBox.Properties.Items.Clear();
            foreach (var item in bll.GetPaperFaceList())
            {
                _comboBox.Properties.Items.Add(item.Name);
            }
        }
        public void LinkPaperType()
        {
            PaperTypeBLL bll = new PaperTypeBLL();
            _comboBox.Properties.Items.Clear();
            foreach (var item in bll.GetPaperTypeList())
            {
                _comboBox.Properties.Items.Add(item.Name);
            }
 
        }

        public void UpdatePaperTypeICC(int printerId,string paperTypename,string iccPath,int sharpValue)
        {
            _bll.UpdatePaperTypeICC(printerId, paperTypename, iccPath, sharpValue);
        }

        public void AddPaperBox(int printerId,int paperBoxNum)
        {
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                List<PaperBox> listPrinterBox =enty.PaperBox.Where(p=>p.printer.PrinterID ==printerId).ToList();
                foreach (var item in listPrinterBox)
                {
                    enty.DeleteObject(item);

                    enty.SaveChanges();
                }

                for (int i = 1; i <= paperBoxNum; i++)
                {
                    PaperBox paperBox = new PaperBox();
                    paperBox.printer = enty.printer.FirstOrDefault(p => p.PrinterID == printerId);
                    paperBox.PaperBoxName = i.ToString();
                    paperBox.PaperTypeName = "未设定";
                    paperBox.PaperWidth = 0;
                    enty.AddToPaperBox(paperBox);

                }
                enty.SaveChanges();
            }
        
        }
        public string GetPaperBox(int printerId)
        {
            using (newSampleYPDBEntities enty = new newSampleYPDBEntities())
            {
                return enty.PaperBox.Where(p => p.printer.PrinterID == printerId).ToList().Count.ToString();
            }
        }
        #endregion

        internal PrinterInfo GetIccPath(int printerid ,string papertypeName)
        {
            return _bll.GetIccPath(printerid, papertypeName);
        }
    }
}
